package com.sale.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sale.common.core.utils.StringUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.system.domain.SysDown;
import com.sale.system.domain.SysStaffManagement;
import com.sale.system.mapper.SysStaffManagementMapper;
import com.sale.system.service.ISysDocumentService;
import com.sale.system.service.ISysStaffManagementService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * @Author: YSL
 * @Date: 2023-07-05 17:22:26
 * @Desc: 职员管理
 */
@RequiredArgsConstructor
@Service
public class SysStaffManagementServiceImpl implements ISysStaffManagementService {

    /**
     * 职员管理 数据层
     */
    private final SysStaffManagementMapper sysStaffManagementMapper;

    /**
     * 编码规则
     */
    private final ISysDocumentService sysDocumentService;

    /**
     * 分页查询职员列表
     *
     * @param sysStaffManagement
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<SysStaffManagement> selectStaffManagementList(SysStaffManagement sysStaffManagement, PageQuery pageQuery) {
        // 模糊查询
        LambdaQueryWrapper<SysStaffManagement> lambdaQueryWrapper = new LambdaQueryWrapper<SysStaffManagement>()
            .like(StringUtils.isNotBlank(sysStaffManagement.getStaffCode()), SysStaffManagement::getStaffCode, sysStaffManagement.getStaffCode())
            .like(StringUtils.isNotBlank(sysStaffManagement.getStaffName()), SysStaffManagement::getStaffName, sysStaffManagement.getStaffName());
        // 分页
        Page<SysStaffManagement> sysStaffManagementPage = sysStaffManagementMapper.selectPage(pageQuery.build(), lambdaQueryWrapper);
        return TableDataInfo.build(sysStaffManagementPage);
    }

    /**
     * 根据职员id查询职员管理列表
     *
     * @param staffId
     * @return
     */
    @Override
    public SysStaffManagement getStaffManagementById(Long staffId) {
        return sysStaffManagementMapper.selectById(staffId);
    }

    /**
     * 新增职员管理
     *
     * @param sysStaffManagement
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertStaffManagement(SysStaffManagement sysStaffManagement) {
        sysStaffManagement.setCreateBy(LoginHelper.getUsername());
        sysStaffManagement.setCreateTime(new Date());
        // 依据编码规则生成职员编码
        sysStaffManagement.setStaffCode(sysDocumentService.selectAuthorityCoding("system:clerk:list"));
        return sysStaffManagementMapper.insert(sysStaffManagement);
    }

    /**
     * 修改职员管理
     *
     * @param sysStaffManagement
     * @return
     */
    @Override
    public int updateStaffManagement(SysStaffManagement sysStaffManagement) {
        sysStaffManagement.setUpdateTime(new Date());
        sysStaffManagement.setUpdateBy(LoginHelper.getUsername());
        return sysStaffManagementMapper.updateById(sysStaffManagement);
    }

    /**
     * 删除职员管理
     *
     * @param staffId
     * @return
     */
    @Override
    public int deleteStaffManagement(Long staffId) {
        return sysStaffManagementMapper.deleteById(staffId);
    }

    /**
     * 批量删除职员管理
     *
     * @param staffIds
     * @return
     */
    @Override
    public int deleteStaffManagementList(Long[] staffIds) {
        return sysStaffManagementMapper.deleteBatchIds(Arrays.asList(staffIds));
    }

    /**
     * 职员管理列表（不分页）
     *
     * @return
     */
    @Override
    public List<SysDown> downList() {
        List<SysDown> list = new ArrayList<>();
    //    List<HashMap<String, String>> resultMap = new ArrayList<>();
        List<SysStaffManagement> sysStaffManagements = sysStaffManagementMapper.selectList();
        for (SysStaffManagement entity : sysStaffManagements) {
            SysDown sys = new SysDown();
            sys.setLabel(entity.getStaffName());
            sys.setValue(String.valueOf(entity.getStaffId()));
            sys.setCode(entity.getStaffCode());
            list.add(sys);
        }
        return list;
    }
}
